﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LIMS.Web
{
    public partial class AdmMessageManage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                divReply.Visible = Session["mesID"] != null ? true : false;
                rpMessage.DataSource = BLL.MessageManager.SearchMessage(null);
                rpMessage.DataBind();
            }
        }

        protected String StuIdToName(string mesID)
        {
            return String.IsNullOrWhiteSpace(mesID) ? "" : BLL.MessageManager.GetStuNameByMesID(mesID);
        }

        /// <summary>
        /// 将数据库的留言状态改为相应的汉字显示
        /// </summary>
        /// <param name="mesStatus"></param>
        /// <returns></returns>
        protected String StatusCodeToWords(object mesStatus)
        {
            if (mesStatus == null || String.IsNullOrWhiteSpace(mesStatus.ToString()))
            {
                return "未处理";
            }
            return mesStatus.ToString() == "1" ? "已回复" : "未处理";
        }

        /// <summary>
        /// 绑定回复者的姓名
        /// </summary>
        /// <param name="mesAdminID"></param>
        /// <returns></returns>
        protected String adminIdToName(object mesAdminID)
        {
            if (mesAdminID == null || String.IsNullOrWhiteSpace(mesAdminID.ToString()))
            {
                return "";
            }
            return BLL.AdminManager.GetAdmNameByID(mesAdminID.ToString());
        }
        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="strInput">需要截断的字符串</param>
        /// <param name="nLength">截断字符串的长度</param>
        /// <returns></returns>
        protected string SubString(object objInput, int nLength)
        {
            if (objInput == null || String.IsNullOrWhiteSpace(objInput.ToString()))
            {
                return "";
            }
            return objInput.ToString().Length <= nLength ? objInput.ToString() : objInput.ToString().Substring(0, nLength) + "…";
        }

        protected void ddlMessageStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            rpMessage.DataSource = BLL.MessageManager.SearchMessage(ddlMessageStatus.SelectedValue);
            rpMessage.DataBind();
        }

        protected void rpMessage_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "myReply")
            {
                divReply.Visible = true;

                #region 还原原始留言数据，以便于管理员回复

                Session["mesID"] = e.CommandArgument.ToString();
                Int32 mesID = Int32.Parse(e.CommandArgument.ToString());
                LIMS.Model.Messages message = BLL.MessageManager.GetMessageByID(mesID.ToString());
                txtMesDetail.Text = message.mesDetail;
                txtStuName.Text = BLL.MessageManager.GetStuNameByMesID(mesID.ToString());
                txtStuName.ReadOnly = true;
                txtMesTitle.Text = message.mesTitle;
                txtReview.Text = message.mesReply;

                #endregion
            }
            if (e.CommandName == "myDel")
            {
                BLL.MessageManager.DelMessage(Int32.Parse(e.CommandArgument.ToString()));
                rpMessage.DataSource = BLL.MessageManager.SearchMessage(null);
                rpMessage.DataBind();
            }
        }

        /// <summary>
        /// 提交回复的内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (Session["mesID"] != null && !String.IsNullOrWhiteSpace(Session["mesID"].ToString()))
            {
                if (Session["adminID"] == null && String.IsNullOrWhiteSpace(Session["adminID"].ToString()))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请不要非法操作!)';", true);
                    return;
                }
                Int32 mesID = Int32.Parse(Session["mesID"].ToString());

                Model.Messages message = new Model.Messages()
                {
                    mesID = mesID.ToString(),
                    mesReply = txtReview.Text.Trim(),
                    mesAdminID = Session["adminID"].ToString()
                };
                if (BLL.MessageManager.ReplyMessage(mesID, message))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('留言回复成功!');", true);
                    rpMessage.DataSource = BLL.MessageManager.SearchMessage(null);
                    rpMessage.DataBind();
                }
                else
                {
                 
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('系统错误!');", true);
                }
            }
        }


        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtMesDetail.Text = txtMesTitle.Text = txtReview.Text = txtStuName.Text = "";
        }




    }
}